<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\Db;
// 应用公共文件
function adminLog($log_info)
{
    $add['log_time'] = time();
    $add['admin_id'] = session('admin_id');
    $add['log_info'] = $log_info;
    $add['log_ip'] = request()->ip();
    $add['log_url'] = request()->baseUrl();
    Db('admin_log')->insert($add);
}

function create_password($pwd) {
    $salt = rand(1, 9999);
    $pwd = md5(md5($pwd) . $salt);
    return $pwd;
}

/**
 * @param $en_pwd 已加密密码
 * @param $password 未加密密码
 * @param $salt
 */
function password_compare($en_pwd, $password, $salt) {
    $newpwd = md5(md5($password) . $salt);

    if ($en_pwd == $newpwd) {
        return true;
    }
    return false;
}

function getMenuArr(){
    $menuArr = include APP_PATH . 'admin/conf/menu.php';
    $act_list = session('act_list');
    if($act_list != 'all' && !empty($act_list)){
        $right = Db('system_menu')->where("id in ($act_list)")->cache(true)->column('right');
        $role_right = '';
        foreach ($right as $val){
            $role_right .= $val.',';
        }
        foreach($menuArr as $k=>$val){
            foreach ($val['child'] as $j=>$v){
//                foreach ($v['child'] as $s=>$son){
//                    if(strpos($role_right,$son['op'].'@'.$son['act']) === false){
//                        unset($menuArr[$k]['child'][$j]['child'][$s]);//过滤菜单
//                    }
//                }

                if(strpos($role_right,$v['op'].'@'.$v['act']) === false){
                    unset($menuArr[$k]['child'][$j]);//过滤菜单
                }

            }
        }
        foreach ($menuArr as $mk=>$mr){

            if (empty($mr['child'])) {
                unset($menuArr[$mk]['child']);
            }

//            foreach ($mr['child'] as $nk=>$nrr){
//                if(empty($nrr['child'])){
//                    unset($menuArr[$mk]['child'][$nk]);
//                }
//            }
        }
    }
    return $menuArr;
}